<!DOCTYPEHTML>
<html>
<head>
    <meta charset="utf-8">
    <title>取棋子实验</title>
    <style type="text/css"><!------调用css样式列表------->
    </style>
    <script type="text/javascript"> function compute() {
        var total = parseInt(document.getElementById("total").value);
        var get = parseInt(document.getElementById("get").value);
        var testTimes = parseInt(document.getElementById("test").value);
        var everyTimes = parseInt(document.getElementById("times").value);
        if(total <= 1) {
            alert("总棋子数应为大于0的偶数！");
            return;
        }

        if(get > total) {
            alert("取的棋子数不能大于总棋子数！");
            return;
        }
        if(total%2 !== 0 ) {
            alert("棋子总数有误，放入的棋子总数必须为偶数!");
            return;
        }
        //a 棋盘  1黑 0白
    debugger
        var aArr = new Array(total).fill(0);
        var bArr = new Array(total).fill(0);
        var blackCount1 = get;
        var blackCount2 = get;
    debugger
        while (blackCount1 > 0 ) {
            var random1 = parseInt(Math.random() * (total));
            if(aArr[random1] === 0) {
                aArr[random1] = 1;
                blackCount1--;
            }
        }
        while (blackCount2 > 0 ) {
            var random1 = parseInt(Math.random() * (total));
            if(bArr[random1] === 0) {
                bArr[random1] = 1;
                blackCount2--;
            }
        }

        document.write("==============此时左桶情况===================<br/>");
        document.write(aArr+"<br/>");
        document.write("==============此时右桶情况==================<br/>");
        document.write(bArr+"<br/><br/><br/>");


        for (var times=1; times<testTimes+1 ; times++) {
            document.write("==============第"+times +"次实验==================<br/> 两个桶各取"+testTimes+"个<br/>");
            var blackCount = 0;
            var whiteCount = 0;
            var j = testTimes;
            for (var one=1; one<everyTimes+1; one++) {
                var str1 = new Array(j).fill(0);
                var str2 = new Array(j).fill(0);
                for(var i=0;i<j;i++) {
                    var random1 = parseInt(Math.random() * (total));
                    var random2 = parseInt(Math.random() * (total));
                    var thisR1 = aArr[random1];
                    var thisR2 = bArr[random2];
                    str1[i] = thisR1;
                    str2[i] = thisR2;

                }
                var add = 0;
                for(var i=0;i<j;i++){
                    if(str1[i] === 1 || str2[i] === 1) {
                        add++;
                        break;
                    }
                }
                if(add) {
                    blackCount++;
                }else {
                    whiteCount++;
                }
                document.write("第"+one+"次： "+ str1 + "+" + str2 + "<br/>")
            }
            var rateLeft = blackCount;
            var rateRight = 0;
            if(whiteCount !== 0) {
                rateLeft = (blackCount/whiteCount).toFixed(2);
                rateRight = 1;
            }
            document.write("[结果比例] 含有黑子 ： 全为白子 = <b style='font-size: 25px;color: coral'>" +blackCount + " : "+whiteCount +" &#8594;\t"+rateLeft +" : "+ rateRight +"</b><br/><br/>");
        }
    }
    </script>
    <form action="" method="post" name="form1">
        <p style="color:green;font-size:40px;width: 1200px">每 个 桶 放 球 数 量
            <input style="color:#ffa12e;font-size:40px;" type="text" name="fname" id = "total"/></p>
        <p style="color:green;font-size:40px;">单 个桶中，黑球的数量
            <input style="color:#ffa12e;font-size:40px;" type="text" name="lname" id = "get"/></p>
        <p style="color:green;font-size:40px;">每 次 抓 取 的 球 数
            <input style="color:#ffa12e;font-size:40px;" type="text" name="lname" id = "test"/></p>
        <p style="color:green;font-size:40px;">单次实验中，重复的次数
            <input style="color:#ffa12e;font-size:40px;" type="text" name="times" id = "times"/></p>
        <input style="color:black;font-size:40px;" type="button" value="提交" onclick="compute()" />
    </form>
    </body>
</html>

</head>

<body οnkeydοwn="doKeyDown(event)">
<div align="center">
    <!--    <canvas id="Mycanvas" width="600" height="500" style="border-top:2px solid #f00;border-bottom:2px solid green;border-left:2px solid blue;border-right:2px solid #f90;">你的浏览器不支持该功能</canvas><br>-->
    <!--    <div id="msg"></div>-->
    <!--    <input type="button" value="重新开始" onClick="rest()">-->
    <!--    <input type="button" value="游戏说明" onClick="explain()">-->
    <!--    <input type="text" value="" onClick="explain()">-->
    <!--    <input type="text" value="" onClick="explain()">-->
    <!--    <form action="compute()" method="get">-->
    <!--        <p>每个棋盘放棋子数 <input type="text" name="fname" /></p>-->
    <!--        <p>每次取出棋子数量 <input type="text" name="lname" /></p>-->
    <!--        <input type="submit" value="开始" />-->
    <!--    </form>-->
</div>
</body>
</html>